﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class bas_company : BasePermissionPage
{
    SqlDataSource SDS_Company = new SqlDataSource();
    DataView dv = new DataView();
    protected void Page_Load(object sender, EventArgs e)
    {
        //== 1.連結資料庫的連接字串 ConnectionString  ，事先已經寫在 Web.Config檔案裡面了==
        SDS_Company.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SDS_Company.ProviderName = "Oracle.DataAccess.Client";
        SDS_Company.CancelSelectOnNullParameter = false;
        
        if (!IsPostBack)
        {
            //DBINIT();
        }
        else
        {

        }
    }
    protected void DBINIT()
    {
        try
        {
            string v_COMPANY_ID, v_SQL, v_COMPANY_NAME;
            DataSourceSelectArguments args = new DataSourceSelectArguments();


            v_COMPANY_ID = txtCOMPANY_ID.Text.Trim();
            v_COMPANY_NAME = txtCOMPANY_NAME.Text.Trim();
            //== 2.撰寫SQL指令 ==
            v_SQL = "SELECT COMPANY_ID,COMPANY_NAME,PARENT_COMPANY_ID  FROM MIS_COMPANY_AP  ";
            v_SQL = v_SQL + " Where (:COMPANY_ID is null or COMPANY_ID = :COMPANY_ID) and (COMPANY_NAME like :COMPANY_NAME)";
            v_SQL = v_SQL + " order by COMPANY_ID ";
            SDS_Company.SelectParameters.Clear();
            SDS_Company.SelectCommand = v_SQL;
            SDS_Company.SelectParameters.Add("COMPANY_ID", TypeCode.String, v_COMPANY_ID);
            SDS_Company.SelectParameters.Add("COMPANY_ID", TypeCode.String, v_COMPANY_ID);
            SDS_Company.SelectParameters.Add("COMPANY_NAME", TypeCode.String, "%" + v_COMPANY_NAME + "%");

            //== 3.執行SQL指令 .select() ==
            SDS_Company.DataSourceMode = SqlDataSourceMode.DataSet;

            dv = (DataView)SDS_Company.Select(args);
            //dv.AddNew();
            DV1.DataSource = dv;
            

            DV1.DataBind();

            ;
        }
        catch (Exception e)
        {
            //Response.Write(e.Message);
            lbl_Message.Text = e.Message;

        }
    }
    protected void DV1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {

            DV1.EditIndex = e.NewEditIndex;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        try
        {
            lbl_Message.Text = "";
            DV1.EditIndex = -1;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        try
        {

            DV1.EditIndex = e.NewEditIndex;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int index;
        string v_COMPANY_ID, v_COMPANY_NAME, v_PARENT_COMPANY_ID;
        TextBox TB = new TextBox();
        try
        {
            index = e.ItemIndex;
            v_COMPANY_ID = (string)DV1.DataKeys[index].Value;
            // EmptyDataTemplate
            v_COMPANY_NAME = ((TextBox)DV1.EditItem.FindControl("txtCOMPANY_NAME")).Text.Trim();
            v_PARENT_COMPANY_ID = ((TextBox)DV1.EditItem.FindControl("txtPARENT_COMPANY_ID")).Text.Trim();
            TB = ((TextBox)DV1.EditItem.FindControl("txtPARENT_COMPANY_ID"));
            lbl_Message.Text = string.Empty;
            if (check(v_PARENT_COMPANY_ID) == false)
            {
                lbl_Message.Text = "上層公司代號必需存在於公司代號檔中!";
                TB.Focus();
                e.Cancel = true;
            }
            else
            {

                //'==下面的程式，只是作SQL指令的更新動作而已
                //'== 設定SQL指令將會用到的參數 ==
                SDS_Company.SelectParameters.Clear();
                SDS_Company.UpdateParameters.Add("COMPANY_NAME", v_COMPANY_NAME);
                SDS_Company.UpdateParameters.Add("PARENT_COMPANY_ID", v_PARENT_COMPANY_ID);
                SDS_Company.UpdateParameters.Add("UPD_ID", User.Identity.Name);
                SDS_Company.UpdateParameters.Add("UPD_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_Company.UpdateParameters.Add("UPD_TIME", System.DateTime.Now.ToString("HHmmss"));
                SDS_Company.UpdateParameters.Add("COMPANY_ID", v_COMPANY_ID);

                SDS_Company.UpdateCommand = "UPDATE MIS_COMPANY_AP SET COMPANY_NAME = :COMPANY_NAME, PARENT_COMPANY_ID = :PARENT_COMPANY_ID, UPD_ID = :UPD_ID, UPD_DATE = :UPD_DATE, UPD_TIME = :UPD_TIME WHERE COMPANY_ID = :COMPANY_ID";
                SDS_Company.Update();
                //'==========================================
                //'==== 離開編輯模式 ====
                DV1.EditIndex = -1;
                DBINIT();
            }




        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int index;
        string v_COMPANY_ID;
        try
        {
            index = e.ItemIndex;
            v_COMPANY_ID = (string)DV1.DataKeys[index].Value;

            //'==下面的程式，只是作SQL指令的更新動作而已
            //'== 設定SQL指令將會用到的參數 ==
            SDS_Company.DeleteParameters.Add("COMPANY_ID", v_COMPANY_ID);
            SDS_Company.DeleteCommand = "Delete MIS_COMPANY_AP Where COMPANY_ID = :COMPANY_ID";
            SDS_Company.Delete();
            DBINIT();

        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        DBINIT();
        lbl_Message.Text = string.Empty;
        int PageSize = ((DataPager)DV1.FindControl("DataPager1")).PageSize;
        ((DataPager)DV1.FindControl("DataPager1")).SetPageProperties(0, PageSize, true);
        
    }
    protected void DV1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string v_COMPANY_ID, v_COMPANY_NAME, v_PARENT_COMPANY_ID;
        TextBox TB = new TextBox();
        try
        {
            v_COMPANY_ID = ((TextBox)DV1.InsertItem.FindControl("txtCOMPANY_ID")).Text.Trim();
            // EmptyDataTemplate
            v_COMPANY_NAME = ((TextBox)DV1.InsertItem.FindControl("txtCOMPANY_NAME")).Text.Trim();
            v_PARENT_COMPANY_ID = ((TextBox)DV1.InsertItem.FindControl("txtPARENT_COMPANY_ID")).Text.Trim();

            TB = ((TextBox)DV1.InsertItem.FindControl("txtPARENT_COMPANY_ID"));
            lbl_Message.Text = string.Empty;
            if (check(v_PARENT_COMPANY_ID) == false)
            {
                lbl_Message.Text = "上層公司代號必需存在於公司代號檔中!";
                TB.Focus();
                e.Cancel = true;
            }
            else
            {

                //'==下面的程式，只是作SQL指令的更新動作而已
                //'== 設定SQL指令將會用到的參數 ==
                SDS_Company.InsertParameters.Clear();

                SDS_Company.InsertParameters.Add("COMPANY_ID", v_COMPANY_ID);
                SDS_Company.InsertParameters.Add("COMPANY_NAME", v_COMPANY_NAME);
                SDS_Company.InsertParameters.Add("PARENT_COMPANY_ID", v_PARENT_COMPANY_ID);
                SDS_Company.InsertParameters.Add("CRT_ID", User.Identity.Name);
                SDS_Company.InsertParameters.Add("CRT_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_Company.InsertParameters.Add("CRT_TIME", System.DateTime.Now.ToString("HHmmss"));
                SDS_Company.InsertParameters.Add("UPD_ID", User.Identity.Name);
                SDS_Company.InsertParameters.Add("UPD_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_Company.InsertParameters.Add("UPD_TIME", System.DateTime.Now.ToString("HHmmss"));


                SDS_Company.InsertCommand = "insert into mis_company_ap (company_id, company_name, parent_company_id, crt_id, crt_date, crt_time, upd_id, upd_date, upd_time) values(:company_id, :company_name, :parent_company_id, :crt_id, :crt_date, :crt_time, :upd_id, :upd_date, :upd_time)";
                SDS_Company.Insert();
                //'==========================================
                DBINIT();
            }

        }
        
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
            //PK重覆
            if (ex.Message.IndexOf("ORA-00001") >= 0)
            {
                lbl_Message.Text = "輸入的公司代號已存在公司檔中！";
            }
            else
            {
                lbl_Message.Text = ex.Message;
            }
            
        }
    }

    protected void DV1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DV1.SelectedIndex = -1;

    }
    protected void DV1_PagePropertiesChanged(object sender, EventArgs e)
    {
        int PageSize = ((DataPager)DV1.FindControl("DataPager1")).PageSize;
        int StartRowIndex = ((DataPager)DV1.FindControl("DataPager1")).StartRowIndex;
        int TotalRowCount = ((DataPager)DV1.FindControl("DataPager1")).TotalRowCount;
        ((DataPager)DV1.FindControl("DataPager1")).SetPageProperties(StartRowIndex, PageSize, true);
        DBINIT();
    }
    
     protected Boolean check(string p_PARENT_COMPANY_ID) 
     {
         //判斷PARENT_COMPANY_ID必需存在COMPANY_ID
         if (p_PARENT_COMPANY_ID != string.Empty)
        {
            DataSourceSelectArguments args = new DataSourceSelectArguments();
            DataView DV = new DataView();

            SDS_Company.SelectCommand = "SELECT COMPANY_ID FROM MIS_COMPANY_AP Where COMPANY_ID = :COMPANY_ID ";
            SDS_Company.SelectParameters.Clear();
            SDS_Company.SelectParameters.Add("COMPANY_ID", TypeCode.String, p_PARENT_COMPANY_ID);

            //== 3.執行SQL指令 .select() ==
            SDS_Company.DataSourceMode = SqlDataSourceMode.DataSet;
            DV = (DataView)SDS_Company.Select(args);
            if (DV.Table.Rows.Count == 0)
            {
                return false;
            }
            
        }
         return true;
     }
     protected void btn_exist_Click(object sender, EventArgs e)
     {
         Response.Redirect("~/Default.aspx");
     }
}